package httpHandler

import (
	baseHttp "gitee.com/zaiqiang231/go-base-app/base_app/http"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/http/service"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/model/req"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetDistrictTreeTotal(c *gin.Context) {
	var request req.DistrictTreeReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	tree, err := service.ServiceDistrictInstance.GetTreeTotal(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"tree": tree,
	})
}

func GetAdCodeList(c *gin.Context) {
	var request req.AdCodeListReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	list, err := service.ServiceDistrictInstance.GetAdCodeList(request.AdCode)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", list)
}

func GetDistrictTreePartial(c *gin.Context) {
	var request req.DistrictTreeReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	tree, err := service.ServiceDistrictInstance.GetTreePartial(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"tree": tree,
	})
}

func GetProvinceAndCityList(c *gin.Context) {
	provinceList, cityList, err := service.ServiceDistrictInstance.GetProvinceAndCityList()
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"provinces": provinceList,
		"cities":    cityList,
	})
}
